package com.runrun.com.runrun.sqlsession;

import com.runrun.pojo.Configuration;
import com.runrun.pojo.MappedStatement;

import java.beans.IntrospectionException;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    public <T> List<T> selectList(String statementId, Object...params) throws IllegalAccessException, IntrospectionException, InstantiationException, NoSuchFieldException, SQLException, InvocationTargetException, ClassNotFoundException {
        SimplleExecutor simplleExecutor = new SimplleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<T> queryList = simplleExecutor.Query(configuration, mappedStatement, params);
        return (List<T>) queryList;
    }

    public <T> T selectOne(String statementId, Object... params) throws IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        List<T> selectList = selectList(statementId,params);
        if(selectList.size()==1){
            return (T) selectList.get(0);
        }else{
            throw new RuntimeException("没有数据或查出数据超过一条");
        }
    }

    public Integer insert(String statementId, Object... params) throws Exception {
        SimplleExecutor simplleExecutor = new SimplleExecutor();
        Integer insertNum = simplleExecutor.insert(configuration, configuration.getMappedStatementMap().get(statementId), params);
        return insertNum;
    }

    public Integer update(String statementId, Object... params) throws Exception {
        SimplleExecutor simplleExecutor = new SimplleExecutor();
        Integer updateNum = simplleExecutor.update(configuration, configuration.getMappedStatementMap().get(statementId), params);
        return updateNum;
    }

    public Integer delete(String statementId, Object... params) throws Exception {
        SimplleExecutor simplleExecutor = new SimplleExecutor();
        Integer deleteNum = simplleExecutor.delete(configuration, configuration.getMappedStatementMap().get(statementId), params);
        return deleteNum;
    }

    public <T> T getMapper(Class<?> mapperClass) {
        Object o = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class<?>[]{mapperClass}, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodeName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className+"."+methodeName;
                Type genericReturnType = method.getGenericReturnType();
                // 判断是否进行了 泛型类型参数化
                if(genericReturnType instanceof ParameterizedType){
                    List<Object> objects = selectList(statementId, args);
                    return objects;
                }

                return selectOne(statementId,args);
            }
        });
        return (T) o;
    }

    public <T> T getInsertMapper(Class<?> mapperClass) {
        Object o = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class<?>[]{mapperClass}, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodeName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className+"."+methodeName;
                Type genericReturnType = method.getGenericReturnType();
                // 判断是否进行了 泛型类型参数化
                Integer insert = insert(statementId,args);
                return insert;
            }
        });
        return (T) o;
    }

    public <T> T getUpdateMapper(Class<?> mapperClass) {
        Object o = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class<?>[]{mapperClass}, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodeName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className+"."+methodeName;
                Type genericReturnType = method.getGenericReturnType();
                // 判断是否进行了 泛型类型参数化
               /* if(genericReturnType instanceof ParameterizedType){
                    List<Object> objects = selectList(statementId, args);
                    return objects;
                }*/
                Integer update = update(statementId,args);
                return update;
            }
        });
        return (T) o;
    }

    public <T> T getDeleteMapper(Class<?> mapperClass) {
        Object o = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class<?>[]{mapperClass}, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodeName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className+"."+methodeName;
                Type genericReturnType = method.getGenericReturnType();
                // 判断是否进行了 泛型类型参数化
               /* if(genericReturnType instanceof ParameterizedType){
                    List<Object> objects = selectList(statementId, args);
                    return objects;
                }*/

                return delete(statementId,args);
            }
        });
        return (T) o;
    }

}
